package org.cqut.system.clazz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.cqut.framework.web.response.ResultInfo;
import org.cqut.framework.web.response.Status;
import org.cqut.system.clazz.domain.Clazz;
import org.cqut.system.clazz.service.ClazzService;
import org.cqut.system.clazz.vo.ClassVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author XiAoXu
 * @date 2021/11/2 13:39
 * @description
 */
@RestController
@RequestMapping("/class")
public class ClazzController {
    private final ClazzService clazzService;

    public ClazzController(ClazzService clazzService) {
        this.clazzService = clazzService;
    }

    @GetMapping(path = "/details")
    public ResultInfo queryClassList(@RequestParam("page") int page, @RequestParam("limit") int limit, @RequestParam("condition") String condition) {
        IPage<ClassVo> classList = clazzService.getClassVoList(page, limit, condition);
        if (classList != null) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message, classList);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }

    @GetMapping("/nameList")
    public ResultInfo queryClassNameList(){
        List<Clazz> list = clazzService.list();
        if (list != null) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message, list);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }


    @PostMapping(path = "/del/{classId}")
    public ResultInfo deleteClassById(@PathVariable Long classId) {
        if (classId <= 0) {
            return new ResultInfo(Status.REQUEST_PARAMETER_ERROR.code, Status.REQUEST_PARAMETER_ERROR.message);
        }
        boolean flag = clazzService.removeById(classId);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @PostMapping("/addOne")
    public ResultInfo saveClass(@RequestBody Clazz cls) {
        if (Objects.isNull(cls)) {
            return new ResultInfo(Status.SERVER_RECEIVE_ERROR.code, Status.SERVER_RECEIVE_ERROR.message);
        }
        boolean flag = clazzService.save(cls);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @PostMapping("/update")
    public ResultInfo updateClassById(@RequestBody Clazz cls) {
        if (Objects.isNull(cls.getClassId())) {
            return new ResultInfo(Status.REQUEST_PARAMETER_ERROR.code, Status.REQUEST_PARAMETER_ERROR.message);
        }
        boolean flag = clazzService.updateById(cls);
        if (flag) {
            return new ResultInfo(Status.UPDATE_DETAILS_SUCCESS.code, Status.UPDATE_DETAILS_SUCCESS.message);
        } else {
            return new ResultInfo(Status.UPDATE_DETAILS_ERROR.code, Status.UPDATE_DETAILS_ERROR.message);
        }
    }
}
